<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class register extends CI_Controller {
	var $data;
	public function __construct()
	{
		parent::__construct();		
		$this->load->library('session');
		$this->load->model('model_register');
		$this->data['title'] = 'Register';
	}
	
	public function index()
	{

		$check = $this->session->userdata('CHECK_LOGIN');
		if($check==true){
			redirect(base_url().'dashboard/', 'refresh');
		}else{
			$this->view();
		}
		
	}

	public function view(){		
		if(isset($_POST["submit"])){
			$username=trim($_POST["username"]);
			$password=trim($_POST["password"]);
			$email=trim($_POST["email"]);

			if($username==""){
				$this->data['msg']="Username not null.";
			}else if($password==""){
				$this->data['msg']="Password not null.";
			}else if($email==""){
				$this->data['msg']="Email not null.";
			}else{
				// Check Tồn tại username					
				$result = $this->model_register->selectExists(array('col'=>'mb_username','val'=>$username));						
				if(count($result)<=0){
					// Check Tồn tại email
					$result= $this->model_register->selectExists(array('col'=>'mb_email','val'=>$email));						
					if(count($result)<=0){
						$this->model_register->insert(array('mb_email'=>$email,'mb_password'=>md5($password),'mb_username'=>$username,'mb_status'=>1));	
						$this->data['msg']="Register success.";
						$this->data['st'] =true;
					}else{
						$this->data['msg']="Email is already used.";
					}
				}else{
					$this->data['msg']="Username is already used.";
				}
			}
			
		}
		
		$this->load->view('register', $this->data);


	}
}

